//
//  SyncImageView.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SyncImageView.h"
#import "AppDelegate.h"
#import "ASIHTTPRequest.h"

@implementation SyncImageView
@synthesize imageUrl;
@synthesize requestQueue;
@synthesize contentView;
@synthesize index;

- (void)initSubviews {
    //indebug = YES;
    indebug = NO;
    self.backgroundColor = [UIColor clearColor];
    
    imageView = [[UIImageView alloc] init];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //imageView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:imageView];
    [imageView release];
    
    message = [[UILabel alloc] init];
    message.textAlignment = UITextAlignmentCenter;
    [self addSubview:message];
    [message release];
    
	waitView = [[UIActivityIndicatorView alloc] init];
    waitView.hidesWhenStopped = YES;
    [self addSubview:waitView];
    [waitView release];
}

- (id)init {
    self = [super init];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (id)initWithUrl:(NSString *)url andRequestQueue:(NSOperationQueue *)queue
{
    self = [super init];
    if (self) {
        [self initSubviews];
        self.imageUrl = url;
        self.requestQueue = queue;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame = self.bounds;
    imageView.frame = frame;
    imageView.hidden = YES;
    
    int x = (frame.size.width - 32) / 2;
    int y = (frame.size.height - 32) / 2;
    waitView.frame = CGRectMake(x, y, 32, 32);
    
    y = (frame.size.height - 21) / 2;
    message.frame = CGRectMake(0, y, frame.size.width, 21);
    message.hidden = YES;
}

- (void)dealloc {
    [contentView release];
    [requestQueue release];
    [imageUrl release];
    [super dealloc];
}

- (void)loadImage:(NSString *)url andRequestQueue:(NSOperationQueue *)queue {
    self.imageUrl = url;
    self.requestQueue = queue;
    [self setNeedsDisplay];
}

- (void)initSyncImageRequest {
    imageView.hidden = YES;
    message.hidden = YES;
    NSString *imagePath = [AppDelegate getCacheFilePathByUrl:imageUrl];
    if ([AppDelegate checkFileExist:imagePath]) {
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        if (image) {
            imageView.image = image;
            imageView.hidden = NO;
            return;
        }
    }
    //初始化等待view
    [waitView startAnimating];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageUrl]];
    [request setDownloadDestinationPath:imagePath];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestComplete:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [requestQueue addOperation:request];
}

- (void)requestComplete:(ASIHTTPRequest *)request {
    [waitView stopAnimating];
    NSString *imagePath = [request downloadDestinationPath];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    if (image) {
        imageView.image = image;
        imageView.hidden = NO;
    } else if (indebug) {
        message.text = @"No Image";
        message.hidden = NO;
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    [waitView stopAnimating];
    NSLog(@"Image Request Failed! path=%@", [request url]);
    if (indebug) {
        message.text = @"Request Failed";
        message.hidden = NO;
    }
}

///*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    if (imageUrl != nil && ![@"" isEqualToString:imageUrl]) {
        //NSLog(@"initSyncImageRequest run. imageUrl=%@", imageUrl);
        [self initSyncImageRequest];
    }
}
//*/

@end
